package com.hcz.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.hcz.dao.UserDao;
import com.hcz.entity.User;
import com.hcz.util.Const;

public class AccountServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String m = request.getParameter("p");
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute(Const.SESSION_USER);
		
		UserDao ud = new UserDao();
		System.out.println(m);
		if("left".equals(m)){
			response.sendRedirect("showLeft.jsp");
			return;
		}else if("save".equals(m)){
			String savemoney = request.getParameter("savemoney");
			user.setMoney(user.getMoney()+Float.parseFloat(savemoney));
			ud.updateMoney(user);
			response.sendRedirect("main.jsp");
			return ;
		}else if("get".equals(m)){
			String savemoney = request.getParameter("savemoney");
			if(Float.parseFloat(savemoney) >  user.getMoney()){
				response.sendRedirect("get.jsp?code=1003");
				return ;
			}
			user.setMoney(user.getMoney()-Float.parseFloat(savemoney));
			ud.updateMoney(user);
			response.sendRedirect("main.jsp");
			return ;
		}else if("tran".equals(m)){
			String name = request.getParameter("account");
			User otherUser = ud.selectByName(name);
			if(otherUser == null){
				response.sendRedirect("trans.jsp?p=re");
				return ;
			}
			String temp = request.getParameter("money");
			float money =Float.parseFloat(temp);
			if(money>user.getMoney()){
				response.sendRedirect("trans.jsp?p=lack");
				return ;
			}
			 user.setMoney(user.getMoney()- money);
			if((ud.updateMoney(user))> 0){
				otherUser.setMoney(otherUser.getMoney()+money); 
				if(ud.updateMoney(otherUser)>0){
					response.sendRedirect("main.jsp");
					return ;
				}
			}else {
				 user.setMoney(user.getMoney()+ money);
				response.sendRedirect("trans.jsp?p=1001");
				return ;
			}
		}else if("edit".equals(m)){
			String opwd = request.getParameter("oldP");
			String npwd = request.getParameter("newP");
			if(!user.getPassword().equals(opwd)){
				response.sendRedirect("edit.jsp?p=wrong");
				return ;
			}
			if(opwd.equals(npwd)){
				response.sendRedirect("edit.jsp?p=re");
				return ;
			}
			user.setPassword(npwd);
			if( ud.updatePwd(user)>0){
				response.sendRedirect("main.jsp");
				return ;
			}else {
				response.sendRedirect("edit.jsp?p=fail");
				return ;
			}
		}else if("all".equals(m)){
			List<User> list = ud.selectForAll();
			if(list == null){
				response.sendRedirect("index.jsp?code=1003");
				return ;
			}
			ServletContext application = getServletContext();
			application.setAttribute("userList", list);
			response.sendRedirect("showAll.jsp");
		}else if("free".equals(m)){
			List<User> list = ud.selectForAll();
			if(list == null){
				response.sendRedirect("index.jsp?code=1003");
				return ;
			}
			ServletContext application = getServletContext();
			application.setAttribute("userList", list);
			response.sendRedirect("free.jsp");
		}else if("refresh".equals(m)){
			String name = request.getParameter("username");
			User tempUser =ud.selectByName(name); 
			if(tempUser==null){
				response.sendRedirect("free.jsp?p=1001");
				return ;
			}
			String state = request.getParameter("state");
			tempUser.setState(Boolean.parseBoolean(state));
			if(ud.updateState(tempUser) >0 ){
				response.sendRedirect("sysCenter.jsp");
				return ;
			}else {
				response.sendRedirect("free.jsp?p=1002");
				return ;
			}
			
		}else if("open".equals(m)){
			List<User> list = ud.selectForAll();
			if(list == null){
				response.sendRedirect("index.jsp?code=1003");
				return ;
			}
			ServletContext application = getServletContext();
			application.setAttribute("userList", list);
			response.sendRedirect("close.jsp");
			
		}else if("close".equals(m)){
			String name = request.getParameter("username");
			User tempUser =ud.selectByName(name); 
			if(tempUser==null){
				response.sendRedirect("close.jsp?p=1001");
				return ;
			}
			String state = request.getParameter("state");
			tempUser.setState(Boolean.parseBoolean(state));
			if(ud.updateState(tempUser) >0 ){
				response.sendRedirect("sysCenter.jsp");
				return ;
			}else {
				response.sendRedirect("close.jsp?p=1002");
				return ;
			}
			
		}else {
			response.sendError(404,"请求一个不存在的路径");
			return ;
		}
		
	}

}
